package com.example.take_out_dish.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.take_out_dish.common.PageResult;
import com.example.take_out_dish.common.lang.constant.StatusConstant;
import com.example.take_out_dish.common.lang.dto.SetmealDTO;
import com.example.take_out_dish.common.lang.dto.SetmealPageQueryDTO;
import com.example.take_out_dish.common.lang.vo.DishItemVO;
import com.example.take_out_dish.common.lang.vo.SetmealVO;
import com.example.take_out_dish.entity.Dish;
import com.example.take_out_dish.entity.Setmeal;
import com.example.take_out_dish.entity.SetmealDish;
import com.example.take_out_dish.exceptions.BusinessException;
import com.example.take_out_dish.exceptions.enumetation.ResponseEnum;
import com.example.take_out_dish.mapper.DishMapper;
import com.example.take_out_dish.mapper.SetmealMapper;
import com.example.take_out_dish.service.DishService;
import com.example.take_out_dish.service.SetmealDishService;
import com.example.take_out_dish.service.SetmealService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;


/**
 * <p>
 * 套餐 服务实现类
 * </p>
 */
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private SetmealDishService setmealDishService;

    @Resource
    private DishService dishService;

    @Resource
    private DishMapper dishMapper;

    /**
     * 根据categoryId查询套餐
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<Setmeal>()
                .eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        List<Setmeal> list = this.list(wrapper);
        return list;
    }

    /**
     * 新增套餐
     * @param source
     */
    @Override
    public void saveWithDish(SetmealDTO source) {
        Setmeal setmeal = new Setmeal();
        BeanUtil.copyProperties(source, setmeal, "createTime", "updateTime");

        //新增套餐
        setmealMapper.insert(setmeal);

        log.info("setmeal.id:{}", setmeal.getId());

        //新增菜品与套餐数据
        List<SetmealDish> setmealDishS = source.getSetmealDishes();
        setmealDishS.forEach(s -> {
            s.setSetmealId(setmeal.getId());
        });
        //保存套餐和菜品的关联关系
        setmealDishService.insertBatch(setmealDishS);
    }

    /**
     * 套餐分页查询
     * @param source
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO source) {
        Page<SetmealVO> page = new Page<>(source.getPage(), source.getPageSize());
        setmealMapper.pageWithCategory(page, source);
        return new PageResult(page.getTotal(), page.getRecords());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void delete(List<String> ids) {
        List<Setmeal> setmealS = setmealMapper.selectBatchIds(ids);
        setmealS.forEach(s->{
            if(s.getStatus().equals(StatusConstant.ENABLE)){
                //起售中的套餐不能删除
                throw new BusinessException(ResponseEnum.SETMEAL_ON_SALE);
            }
        });
        //删除套餐
        setmealMapper.deleteBatchIds(ids);
        //删除套餐菜品关系表中的数据
        setmealDishService.deleteBatchBySetmealId(ids);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(String id) {
        Setmeal setmeal = setmealMapper.selectById(id);


        SetmealVO setmealVo = new SetmealVO();
        BeanUtil.copyProperties(setmeal, setmealVo, "createTime", "updateTime");

        List<SetmealDish> setmealDishS = setmealDishService.selectBySetmealId(id);
        setmealVo.setSetmealDishes(setmealDishS);
        return setmealVo;
    }

    /**
     * 修改套餐
     * @param source
     */
    @Override
    public void update(SetmealDTO source) {
        Setmeal setmeal = new Setmeal();
        BeanUtil.copyProperties(source, setmeal, "createTime", "updateTime");

        //修改套餐表
        setmealMapper.updateById(setmeal);
        //删除套餐和菜品的关联关系
        setmealDishService.deleteBatchBySetmealId(Collections.singletonList(setmeal.getId()));
        //重新插入套餐和菜品的关联关系
        List<SetmealDish> setmealDishS = source.getSetmealDishes();
        setmealDishS.forEach(s->{
            s.setSetmealId(setmeal.getId());
        });
        //保存套餐和菜品的关联关系
        setmealDishService.insertBatch(setmealDishS);
    }

    /**
     * 套餐起售停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, String id) {
        //起售套餐时,判断套餐内是否有停售的菜品
        if (status.equals(StatusConstant.ENABLE)){
            List<Dish> dishS = dishService.selectBySetmealId(id);
            dishS.forEach(s->{
                if (s.getStatus().equals(StatusConstant.DISABLE)){
                    throw new BusinessException(ResponseEnum.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal = setmealMapper.selectById(id);
        setmeal.setStatus(status);
        setmealMapper.updateById(setmeal);
    }
}
